package com.example.android.thread;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;

import com.example.android.thread.init.FutureTaskActivity;
import com.example.android.thread.init.RunnableActivity;
import com.example.android.thread.init.ThreadActivity;
import com.example.android.thread.init.ThreadPoolActivity;
import com.example.android.thread.model.FirstLevelInstance;
import com.example.android.thread.views.ExpandableListviewAdapter;
import com.example.androidthread.R;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    //添加一级数据项 存储一级菜单的名字
    private ArrayList<String> firstLevelMenuList = new ArrayList<>();

    //二级数据
    private  LinkedHashMap<String, List<String>> secondItems = new LinkedHashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();

        ExpandableListView expandableListView = findViewById(R.id.expandableListView);
        ExpandableListviewAdapter expandableListAdapter = new ExpandableListviewAdapter(this, firstLevelMenuList, secondItems);
        expandableListView.setAdapter(expandableListAdapter);
        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                //Toast.makeText(getApplicationContext(),"groupPosition:" + groupPosition + " childPosition:" + childPosition + " id:" + childPosition,Toast.LENGTH_LONG).show();
                if (groupPosition == 0){ //线程初始化
                    initThread(childPosition);
                }
                return false;
            }
        });

    }

    private void initThread(int typeInit){
        switch (typeInit){
            case 0:
              startThreadActivity(ThreadActivity.class);
                break;
            case 1:
                startThreadActivity(RunnableActivity.class);
                break;
            case 2:
                startThreadActivity(FutureTaskActivity.class);
                break;
            case 3:
                startThreadActivity(ThreadPoolActivity.class);
                break;

        }
    }

    private void startThreadActivity(Class<?> compatActivityClass){
        Intent intent = new Intent(this,compatActivityClass);
        startActivity(intent);
    }

    private   void initData(){
        firstLevelMenuList.add(FirstLevelInstance.ThreadInitialization);
        //线程初始化化
        List<String> threadInitializationList = new ArrayList<>();
        threadInitializationList.add("继承Thread");
        threadInitializationList.add("实现Runnable接口");
        threadInitializationList.add("实现Callable接口+ FutureTask");
        threadInitializationList.add("线程池");
        secondItems.put(FirstLevelInstance.ThreadInitialization,threadInitializationList);
    }
}